﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
using DBUtility;
using Model;

namespace DAL
{
    public class TimeLineDAL
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="extsql">sql条件</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageCount">条数</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="totalItems">总条数</param>
        /// <returns></returns>
        public List<TimeLine> GetPage(string extsql, int pageIndex, int pageCount, ref int totalPages, ref int totalItems)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from TimeLine where 1=1 ");
            sql.Append(extsql);
            sql.Append("  order by tl_id desc");
            sql.Append(" limit " + ((pageIndex - 1) * pageCount) + "," + pageCount + ";");
            sql.Append("SELECT COUNT(*) FROM  TimeLine  where 1=1 " + extsql + ";");
            return DBUtility.MySqlHelper.ExecuteDataReaderPage<TimeLine>(sql.ToString(), pageCount, ref totalPages, ref totalItems);
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TimeLine GetModel(string id)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<TimeLine>("select * from TimeLine where tl_id=@id", new MySqlParameter("@id",id));
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            string sql = string.Format("select {0} from TimeLine where 1=1 {1}", column, where);
            return DBUtility.MySqlHelper.ExecuteDynamic(sql);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(TimeLine model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@tl_content",model.tl_content),
                new MySqlParameter("@tl_time",model.tl_time)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery(" insert into TimeLine (tl_content,tl_time) Values(@tl_content,@tl_time)", para);

        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(TimeLine model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@tl_content",model.tl_content),
                new MySqlParameter("@tl_id",model.tl_id)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("update TimeLine set tl_content=@tl_content where tl_id=@tl_id", para);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delete(string id)
        {
            return DBUtility.MySqlHelper.ExecuteNonQuery("delete from TimeLine where tl_id=@tl_id", new MySqlParameter("@tl_id", id));
        }

    }
}
